package com.toKnow.service.impl;

import com.toKnow.mapper.TagPubDao;
import com.toKnow.mapper.TypePubDao;
import com.toKnow.model.entity.Tag;
import com.toKnow.service.TypeTagPubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("typeTagPubService")
public class TypeTagPubServiceImpl implements TypeTagPubService {

    @Autowired
    private TypePubDao typePubDao;
    @Autowired
    private TagPubDao tagPubDao;


    public void setTypePubDao(TypePubDao typePubDao) {
        this.typePubDao = typePubDao;
    }

    public void setTagPubDao(TagPubDao tagPubDao) {
        this.tagPubDao = tagPubDao;
    }

    @Override
    public List<Integer> convertTypeNameToId(List<String> names) {

        List<Integer> Ids = new ArrayList<>();
        if(names != null){
            for(String name : names){
                Integer id =  typePubDao.selectTypeByName(name);
                if(id !=null){
                    Ids.add(id);
                }
            }
            return Ids;
        }else {
            //System.out.println("service return null");
            return null;
        }


        //System.out.println(Ids);

    }

    @Override
    @Transactional
    public List<Integer> convertTagNameToId(List<String> names) {
        List<Integer> Ids = new ArrayList<>();

        if(names!=null){
            for(String name : names){
                //System.out.println(name);
                Integer id =  tagPubDao.selectTagByName(name);
                //System.out.println(id);
                if(id!=null){
                    Ids.add(id);
                } else {
                    Tag newTag = new Tag();
                    newTag.setName(name);
                    tagPubDao.insertTag(newTag);
                    Ids.add(newTag.getId());
                }
            }
            return Ids;
        }else {
            return null;
        }


    }

}
